<template>
  <view class="myPage">
    <scroll-view
      v-if="dataList.length > 0"
      scroll-x="false"
      scroll-y="true"
      @scrolltolower="getList"
    >
      <view class="myList">
        <view
          v-for="(item, index) in dataList"
          :key="index"
          class="list-item"
          @click="selectAddress(item)"
        >
          <view class="left">
            <view class="top">
              <text
                >{{ item.provinceName + item.cityName + item.countyName }}
              </text>
              <text v-if="item.isDefault == 1" class="label">默认</text>
            </view>
            <text class="address-desc">{{ item.address }} </text>
            <view class="address-user">
              <text>{{ item.name }}</text>
              <text>{{ item.phone }}</text>
            </view>
          </view>
          <image
            class="edit"
            src="https://ltjt.sjzballsportpark.com/statics/tyc/static/icon-edit.png"
            @click.stop="toURL(`/page_my/address/addOrEdit?id=${item.id}`)"
          ></image>
        </view>
      </view>
    </scroll-view>
    <view v-else>
      <noData />
    </view>
    <view class="footer-btn" @click="toURL('/page_my/address/addOrEdit')">
      <view class="btn-box">
        <image
          src="https://ltjt.sjzballsportpark.com/statics/tyc/static/icon-add.png"
        ></image>
        <text>新增地址</text>
      </view>
    </view>
  </view>
</template>

<script>
import { getAddressList } from "@/config/api.js";
import noData from "@/components/noData.vue";

export default {
  components: { noData },
  data() {
    return {
      total: 0,
      dataList: [],
      pages: {
        pageNum: 1,
        pageSize: 10,
      },
      loadMore: true,
    };
  },
  props: ["form"],
  computed: {},
  mounted() {},
  onShow() {
    this.loadMore = true;
    this.pages = {
      pageNum: 1,
      pageSize: 10,
    };
    this.dataList = [];
    this.getList();
  },
  methods: {
    selectAddress(item) {
      if (this.form) {
        this.$store.commit("SELECT_ADDRESS_INFO", item);
        uni.navigateBack();
      }
    },
    getList() {
      if (!this.loadMore) {
        return;
      }
      getAddressList({ ...this.pages })
        .then((res) => {
          console.log("获取了list列表数据", res);
          if (res.code === 200) {
            if (res.rows.length > 0) {
              this.dataList.push(...res.rows);
              if (this.dataList.length < res.total) {
                this.loadMore = true;
                this.pages.pageNum = this.pages.pageNum + 1;
              } else {
                this.loadMore = false;
              }
            }
          }
        })
        .catch((err) => console.log("错误了", err));
    },
  },
};
</script>

<style lang="scss" scoped>
.myPage {
  position: relative;
  width: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
}

.myList {
  height: calc(100vh - 152rpx);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 0 40rpx;

  .list-item {
    width: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    background: #ffffff;
    border-bottom: 2rpx solid #f3f3f3;
    padding: 36rpx 0 32rpx;

    .left {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      justify-content: center;

      .top {
        font-size: 24rpx;
        font-weight: 400;
        color: rgba(0, 0, 0, 0.7);
        line-height: 32rpx;
        margin-bottom: 8rpx;

        .label {
          width: 64rpx;
          height: 32rpx;
          padding: 0 8rpx;
          background: #ff5000;
          border-radius: 8rpx 8rpx 8rpx 8rpx;
          text-align: center;
          margin-left: 16rpx;

          font-size: 24rpx;
          font-weight: 400;
          color: #fff201;
          line-height: 32rpx;
        }
      }

      .address-desc {
        font-size: 32rpx;
        font-weight: 600;
        color: rgba(0, 0, 0, 0.8);
        line-height: 44rpx;
        margin-bottom: 8rpx;
      }

      .address-user {
        font-size: 28rpx;
        font-weight: 400;
        color: rgba(0, 0, 0, 0.7);
        line-height: 40rpx;
      }
    }

    .edit {
      height: 40rpx;
      width: 40rpx;
      flex: 0 0 auto;
      margin-left: 42rpx;
    }
  }

  .loadMore {
    display: block;
    width: 100%;
    font-size: 24rpx;
    color: rgba(0, 0, 0, 0.5);
    text-align: center;
    line-height: 64rpx;
  }
}

.footer-btn {
  position: fixed;
  bottom: 0;
  padding-bottom: env(safe-area-inset-bottom);
  box-shadow: 0rpx 14rpx 58rpx 0rpx rgba(100, 100, 111, 0.2);
  background: #FFFFFF;
  height: calc(124rpx + env(safe-area-inset-bottom));
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  .btn-box {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    width: 686rpx;
    height: 80rpx;
    background: #ff5000;
    border-radius: 44rpx 44rpx 44rpx 44rpx;
  }
  image {
    width: 40rpx;
    height: 40rpx;
    flex: 0 0 auto;
  }

  text {
    font-size: 32rpx;
    font-weight: 400;
    color: #ffffff;
    line-height: 32rpx;
  }
}
</style>
